package org.alis.smallcc.exportexcel.util;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Collection;

/**
 * @project: ExportExcelExtUtil.java
 * @description: 扩展导出类
 * @author: smallcc
 * @create: 2019/9/10 :23:13
 */
public class ExportExcelExtUtil<T> extends ExportExcelUtil<T> {

    private static final Logger log = LoggerFactory.getLogger(ExportExcelExtUtil.class);


    /**
     * <p>
     * 导出带有头部标题行的Excel <br>
     * 时间格式默认：yyyy-MM-dd hh:mm:ss <br>
     * </p>
     *
     * @param title    表格标题
     * @param headers  头部标题集合
     * @param dataset  数据集合
     * @param response 输出流
     * @param version  2003 或者 2007，不传时默认生成2003版本
     */
    public void exportExcel(String fileName, String title, String[] headers, Collection<T> dataset, HttpServletResponse response, String version) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
            if (StringUtils.isBlank(version) || EXCEL_FILE_2003.equals(version.trim())) {
                exportExcel2003(title, headers, dataset, response.getOutputStream(), "yyyy-MM-dd HH:mm:ss");
            } else {
                exportExcel2007(title, headers, dataset, response.getOutputStream(), "yyyy-MM-dd HH:mm:ss");
            }
        } catch (Exception e) {
            log.error("导出出错，异常->{}", e);
        }
    }
}
